package server.boot;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;

import po.AccountBookPo;
import db.Database;
import db.accountbook.AccountBookDB;
import db.client.ClientDB;
import db.commodity.CommodityDB;
import db.commodity.CommodityTypeDB;
import db.document.StockScrapAndOverflowDocumentDB;
import db.document.StockWarningDocumentDB;
import db.document.StockSendingDocumentDB;
import db.document.AccountInAndOutDocumentDB;
import db.document.CashExpensesDocumentDB;
import db.document.ImportAndReturnDocumentDB;
import db.document.SalesAndReturnDocumentDB;
import db.finance.AccountorDB;
import db.history.SaleLogDB;
import db.history.SystemLogDB;
import db.history.经营情况DB;
import db.strategy.StrategyDB;
import db.user.UserDB;
import dbservice.accountbook.AccountBookDBService;
import dbservice.client.ClientDBService;
import dbservice.commodity.CommodityDBService;
import dbservice.commodity.CommodityTypeDBService;
import dbservice.document.DocumentDBService;
import dbservice.finance.AccountorDBService;
import dbservice.history.SaleLogDBService;
import dbservice.history.SystemLogDBService;
import dbservice.history.经营情况DBService;
import dbservice.strategy.StrategyDBService;
import dbservice.user.UserDBService;
import vo.AccountBookVo;
import bl.accountbook.AccountBookBL;
import bl.client.ClientBL;
import bl.commodity.CommodityBL;
import bl.commodity.CommodityTypeBL;
import bl.commodity.StockBL;
import bl.document.StockScrapAndOverflowDocumentBL;
import bl.document.StockWarningDocumentBL;
import bl.document.StockSendingDocumentBL;
import bl.document.AccountInAndOutDocumentBL;
import bl.document.CashExpensesDocumentBL;
import bl.document.ImportAndReturnDocumentBL;
import bl.document.SaleAndReturnDocumentBL;
import bl.finance.AccountorBL;
import bl.history.SaleLogBL;
import bl.history.SystemLogBL;
import bl.history.经营情况BL;
import bl.strategy.StrategyBL;
import bl.user.UserBL;
import blservice.accountbook.AccountBookBLService;
import blservice.client.ClientBLService;
import blservice.commodity.CommodityBLService;
import blservice.commodity.CommodityTypeBLService;
import blservice.commodity.StockBLService;
import blservice.document.DocumentBLService;
import blservice.finance.AccountorBLService;
import blservice.history.SaleLogBLService;
import blservice.history.SystemLogBLService;
import blservice.history.经营情况BLService;
import blservice.loader.ServiceLoaderService;
import blservice.strategy.StrategyBLService;
import blservice.user.UserBLService;

public class ServiceLoader extends UnicastRemoteObject implements ServiceLoaderService{

	private static final long serialVersionUID = 4633644506583663840L;
	private static final int serverPort=9587;
	static private ServiceLoader sl;
	private ServiceLoader() throws RemoteException{
	}

	public static void iniServiceLoader() throws RemoteException, MalformedURLException, SQLException, AlreadyBoundException{
		SystemLogDB.iniSystemLogDB();
		LocateRegistry.createRegistry(serverPort);
		if(sl==null){
		sl=new ServiceLoader();		
		}
		sl.loadAccountBookBLService();
		try {
			sl.bind(sl,"ServiceLoaderService");
		} catch (AlreadyBoundException e) {
			e.printStackTrace();
		}
	}
			
	private void bind(Remote remote,String name) throws MalformedURLException, RemoteException, AlreadyBoundException{
		Naming.bind("rmi://localhost:"+serverPort+"/"+name,remote);
	}
	
	private AccountBookPo ABV2P(AccountBookVo vo){
		if(vo==null){
			return Database.getDefaultAB();
		}
		AccountBookPo po=new AccountBookPo();
		po.setName(vo.getName());
		return po;
	}

	@Override
	public void loadAccountBookBLService() throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException , SQLException{
		AccountBookDBService db=new AccountBookDB();
		SystemLogDBService log=new SystemLogDB();
		AccountBookBLService bl=new AccountBookBL(db,log);
		Database.setDefaultAB(ABV2P(bl.getDefaultAccountBook()));
		bind(bl,"AccountBookBLService");
	}

	@Override
	public void loadClientBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException , SQLException{
		ClientDBService db=new ClientDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		ClientBLService bl=new ClientBL(db,log);
		bind(bl,"ClientBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadCommodityBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		CommodityDBService db=new CommodityDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		CommodityBLService bl=new CommodityBL(db,log);
		bind(bl,"CommodityBLService."+ABV2P(ab).getName());
	}

	@Override
	public void loadCommodityTypeBLService(AccountBookVo ab)throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		CommodityTypeDBService db=new CommodityTypeDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		CommodityTypeBLService bl=new CommodityTypeBL(db,log);
		bind(bl,"CommodityTypeBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadStockBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		CommodityDBService db=new CommodityDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		StockBLService bl=new StockBL(db,log,ABV2P(ab));
		bind(bl,"StockBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadAccountorBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		AccountorDBService db=new AccountorDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		AccountorBLService bl=new AccountorBL(db,log);
		bind(bl,"AccountorBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load经营情况BLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		经营情况DBService db=new 经营情况DB(ABV2P(ab));
		经营情况BLService bl=new 经营情况BL(db);
		bind(bl,"经营情况BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadSaleLogBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		SaleLogDBService db=new SaleLogDB(ABV2P(ab));
		SaleLogBLService bl=new SaleLogBL(db);
		bind(bl,"SaleLogBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadSystemLogBLService() throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		SystemLogDBService db=new SystemLogDB();
		SystemLogBLService bl=new SystemLogBL(db);
		bind(bl,"SystemLogBLService");
		
	}

	@Override
	public void loadStrategyBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		StrategyDBService db=new StrategyDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		StrategyBLService bl=new StrategyBL(db, log);
		bind(bl,"StrategyBLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void loadUserBLService() throws RemoteException, MalformedURLException, AlreadyBoundException, SQLException {
		UserDBService db=new UserDB();
		SystemLogDBService log=new SystemLogDB();
		UserBLService bl=new UserBL(db,log);
		bind(bl,"UserBLService");
	}

	@Override
	public void load进退货单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new ImportAndReturnDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new ImportAndReturnDocumentBL(db, log);
		bind(bl,"进退货单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load库存报警单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new StockWarningDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new StockWarningDocumentBL(db, log);
		bind(bl,"库存报警单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load库存报溢报损单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new StockScrapAndOverflowDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new StockScrapAndOverflowDocumentBL(db, log);
		bind(bl,"库存报溢报损单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load库存赠送单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new StockSendingDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new StockSendingDocumentBL(db, log);
		bind(bl,"库存赠送单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load收付款单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new AccountInAndOutDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new AccountInAndOutDocumentBL(db, log);
		bind(bl,"收付款单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load现金费用单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new CashExpensesDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new CashExpensesDocumentBL(db, log);
		bind(bl,"现金费用单BLService."+ABV2P(ab).getName());
		
	}

	@Override
	public void load销退售单BLService(AccountBookVo ab) throws RemoteException,
			MalformedURLException, AlreadyBoundException, SQLException {
		DocumentDBService db=new SalesAndReturnDocumentDB(ABV2P(ab));
		SystemLogDBService log=new SystemLogDB();
		DocumentBLService bl=new SaleAndReturnDocumentBL(db, log);
		bind(bl,"销退售单BLService."+ABV2P(ab).getName());
		
		
	}
}
